//
//  APIService+Store.m
//  JiZhuiAn
//
//  Created by Easy.Lin on 16/7/14.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "APIService+Store.h"

@implementation APIService (Store)

- (RACSignal *)searchCommodityWithKeyWord:(NSString *)keyWord
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        BmobQuery * query = [BmobQuery queryWithClassName:@"Store"];
    
        [query whereKey:@"name" startWithString:keyWord];
        
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            if (!error) {
                [subscriber sendNext:array];
                [subscriber sendCompleted];
            }
            else {
                [subscriber sendError:error];
            }
        }];
        return nil;
    }];
}

- (RACSignal *)getCommodityWithSortType:(NSString *)sortType
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        BmobQuery * query = [BmobQuery queryWithClassName:@"Store"];
        [query orderByDescending:sortType];
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            if (!error) {
                [subscriber sendNext:array];
                [subscriber sendCompleted];
            }
            else {
                [subscriber sendError:error];
            }
        }];
        return nil;
    }];
}

@end
